package com.excilys.training.ebanking.web.controllers;

import java.util.List;

import org.excilys.training.ebanking.iservice.IDateProviderFactory;
import org.excilys.training.ebanking.iservice.IServiceOperation;
import org.excilys.training.ebanking.model.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewControleur {

	@Autowired
	private IServiceOperation serviceOperation;

	@Autowired
	private IDateProviderFactory providerFactory;

	@RequestMapping("client/compte/{id}/{mois}/{year}/liste_Operations")
	public String generatorExcel(@PathVariable("id") int id,
			@PathVariable("mois") int mois, @PathVariable("year") int year,
			ModelMap model) {

		List<Operation> operationsByMonth = serviceOperation
				.getAllCarteOperationByMonth(id, mois, year);

		if (!serviceOperation.getAllNoCarteOperationsByMonth(id, mois, year)
				.isEmpty()) {
			operationsByMonth.addAll(serviceOperation
					.getAllNoCarteOperationsByMonth(id, mois, year));
		}

		model.put("year", year);
		model.put("nomMois", providerFactory.getLisMoisOfYear(mois));
		model.put("operationsByMonth", operationsByMonth);
		model.put("numeroCompte", id);

		return "excelGenerator";
	}

}
